package cn.jee.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.sql.SQLException;
import java.util.concurrent.TimeoutException;

@Controller
public class ExController {
  //局部异常处理，发生和处理在同一个Controller
  @ExceptionHandler(TimeoutException.class)
  @ResponseBody
  public String process(TimeoutException ex){
    return ex.getMessage();

  }
  @RequestMapping("/er_demo")
  public String demo()throws Exception{
    double rnd=Math.random();
    if(rnd<0.25){
      throw new RuntimeException("5xx错误");
    }else if(rnd<0.5){
      throw new TimeoutException("局部异常处理");
    }else if(rnd<0.75){
      throw new IOException("advice全局异常处理");
    }else{
      throw new SQLException("ErrorPage全局异常处理");
    }
  }
}
